package com.epoint.mobileoa.tools;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class HanzhisToPinYin {

	public static String toPinYintwo(String hanzhis) {
		CharSequence hanzhixulie = hanzhis;

		char[] hanzhishuzu = new char[hanzhixulie.length()];

		for (int i = 0; i < hanzhixulie.length(); i++) {
			hanzhishuzu[i] = hanzhixulie.charAt(i);
		}

		char[] t1 = hanzhishuzu; //

		String[] t2 = new String[hanzhixulie.length()];

		/** */
		/**
		 * 设置输出格式
		 */
		net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);

		int t0 = t1.length;
		//System.out.println("hanzhishuzu length:"+t0);
		String py = "";
		try {
			for (int i = 0; i < t0; i++) {
				t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
				py = py + t2[0].toString().substring(0,1);
			}
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}

		return py.trim();
	}
	
	 /** 
     * 获取汉字串拼音首字母，英文字符不变 
     * 
     * @param chinese 汉字串 
     * @return 汉语拼音首字母 
     */ 
    public static String cn2FirstSpell(String chinese) { 
            StringBuffer pybf = new StringBuffer(); 
            char[] arr = chinese.toCharArray(); 
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); 
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); 
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); 
            for (int i = 0; i < arr.length; i++) { 
                    if (arr[i] > 128) { 
                            try { 
                                    String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat); 
                                    if (_t != null) { 
                                            pybf.append(_t[0].charAt(0)); 
                                    } 
                            } catch (BadHanyuPinyinOutputFormatCombination e) { 
                                    e.printStackTrace(); 
                            } 
                    } else { 
                            pybf.append(arr[i]); 
                    } 
            } 
            return pybf.toString().replaceAll("\\W", "").trim(); 
    } 

    //汉子转成拼音
    public static String hanzi2pinyin(String hanzi){
		char[] cname = hanzi.toLowerCase().toCharArray();
		String pinyin = "";
		for(int i=0;i<cname.length;i++){
			if((cname[i]>=97&&cname[i]<=122)||(cname[i]>=48&&cname[i]<=57)){
				pinyin += cname[i];
			}
			else{
				try{
					String[] arr = PinyinHelper.toHanyuPinyinStringArray(cname[i]);
					pinyin += arr[0].substring(0,arr[0].length()-1);
				}catch(Exception e){
					pinyin += cname[i];
				}
			}
		}
		return pinyin;
	}
}
